package com.java1234.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.java1234.model.animaled;
import com.java1234.model.car;

public class carDao {

	public int add(Connection con,String username,String t,String p)throws Exception{
		String sql="insert into db_book.dbo.t_car values(?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, username);
		pstmt.setString(2, t);
		pstmt.setInt(3, Integer.valueOf(p));
		return pstmt.executeUpdate();
	}
	public String find1(Connection con,String username)throws Exception{
		String sql="select count(*) from db_book.dbo.t_car where username like ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		ResultSet rs = null;
		pstmt.setString(1, username);
		rs=pstmt.executeQuery();
		String result = null;
		while(rs.next()) {
			 result = rs.getString(1);
		}
		return result;
	}
	public List<car> find(Connection con,String username)throws Exception{
		List<car> animalList = new ArrayList<car>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sq = "select * from db_book.dbo. t_car where username like ?";
		pstmt = con.prepareStatement(sq);		
		pstmt.setString(1, username);
		rs=pstmt.executeQuery();
		while(rs.next()) {
			animalList.add(new car(rs.getString(1),rs.getString(2),rs.getInt(3)));
		}
		return animalList;
	}
	public int delete(Connection con,String id)throws Exception{
		String sql="delete from db_book.dbo.t_car where id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}

}
